package com.woniu.controller;

import com.woniu.pojo.Subject;
import com.woniu.entity.Result;
import com.woniu.entity.dto.SubjectImportDTO;
import com.woniu.pojo.Trainer;
import com.woniu.service.ISubjectService;
import com.woniu.utils.EasyExcelUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/subject")
public class SubjectController {

    @Autowired
    private ISubjectService subjectService;

    @GetMapping("/list/all")
    public Result<List<Subject>> listAll(){
        List<Subject> subjectList = subjectService.list();
        return Result.ok(subjectList);
    }

    @Caching(evict = {
            @CacheEvict(value = "subject::index:two", allEntries = true)
    })
    @PostMapping("/import")
    public Result importData(MultipartFile file) throws Exception {
        List<SubjectImportDTO> excelData = EasyExcelUtils.readExcelOneSheet(file.getInputStream(), SubjectImportDTO.class);
        boolean flag = subjectService.importData(excelData);
        return Result.ok(flag);
    }

    @PostMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) throws Exception {
        File file = ResourceUtils.getFile("classpath:subject_template.xlsx");
        downloadTemplate("课程分类模板", file, response);
    }

    /**
     * 普通下载模板的方法
     * @param fileName
     * @param file
     * @param response
     * @throws Exception
     */
    public static void downloadTemplate(String fileName, File file, HttpServletResponse response) throws Exception {
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf8");
        response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName + ".xlsx");

        int len = 0;
        byte[] bs = new byte[1024];
        InputStream in = new FileInputStream(file);
        while((len = in.read(bs)) != -1){
            response.getOutputStream().write(bs,0, len);
        }
        in.close();
        response.getOutputStream().flush();
    }

    /**
     * 导出数据
     * @param response
     * @throws Exception
     */
    @PostMapping("/export")
    public void exportData(HttpServletResponse response) throws Exception {
        // 1. 根据条件查询数据（此处暂时不添加条件）
        List<Subject> subjects = subjectService.list();
        // 2. 调用工具类将数据写入到Excel文件中，并将Excel文件响应给浏览器
        EasyExcelUtils.writeWeb("课程分类", Subject.class, subjects,"课程分类数据", response);
    }

    /**
     * 查询课程分类
     * @return
     */
    @GetMapping("/listSubject")
    Result<List<Subject>> listTrainer() {
        List<Subject> subjectList = subjectService.listSubject();
        return Result.ok(subjectList);
    }


}